package com.danskin.wechat.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.danskin.wechat.cache.JedisClient;
import com.danskin.wechat.dao.MenuDao;
import com.danskin.wechat.entity.Menu;
import com.danskin.wechat.service.MenuService;
import com.danskin.wechat.util.JsonUtils;
@Service
public class MenuServiceImpl implements MenuService {
	
	@Autowired
	private MenuDao menuDao;
	
	@Autowired
	private JedisClient jedisClient;
	
	@Value("${MenuCache}")
	private String MenuCache;
	
	/*private String userName=null;*/

	@Override
	public List<Menu> listSubMenuByParentId(String parentId) throws Exception {
		List<Menu> list = menuDao.listSubMenuByParentId(parentId);
		return list;
	}

	
	/**
	 * 获取所有菜单并填充每个菜单的子菜单列表(系统菜单列表)(递归处理)
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<Menu> listAllMenuQx(String MENU_ID) throws Exception {
		List<Menu> menuList = this.listSubMenuByParentId(MENU_ID);
		for(Menu menu : menuList){
			menu.setHasMenu(true);
			menu.setSubMenu(this.listAllMenuQx(menu.getId()+""));
			menu.setTarget("treeFrame");
		}
		return menuList;
	}

	/**
	 * 超级管理员获取菜单
	 */
	@Override
	public List<Menu> listAllMenuByCondtion(Map<String, Object> map) throws Exception {
		List<Menu> list = menuDao.listAllMenuByCondtion(map);
		for(Menu menu : list){
			menu.setHasMenu(true);
			menu.setSubMenu(this.listAllMenuQx(menu.getId()+""));
			menu.setTarget("treeFrame");
		}
		return list;
	}

	/**
	 * 普通管理员获取菜单
	 */
	@Override
	public List<Menu> listOperatorMenuByCondtion(Map<String, Object> map) throws Exception {
		String userName=(String)map.get("userName");
		String menuJson = jedisClient.get(MenuCache+":"+userName);
		jedisClient.expire(MenuCache+":"+userName, 86400);
		if(StringUtils.isNotBlank(menuJson)){
			List<Menu> menuList = JsonUtils.jsonToList(menuJson, Menu.class);
			return menuList;
		}
		
		List<Menu> list = menuDao.listOperatorMenuByCondtion(map);
		for(Menu menu : list){
				map.put("menuId", menu.getId());
				menu.setHasMenu(true);
				menu.setSubMenu(this.listOperatorMenuQX(map));
				menu.setTarget("treeFrame");
			
		}
		jedisClient.set(MenuCache+":"+userName, JsonUtils.objectToJson(list));
		jedisClient.expire(MenuCache+":"+userName, 86400);
		return list;
	}
	
	public List<Menu> listOperatorMenuQX(Map<String, Object> map) throws Exception {
		List<Menu> menuList = this.listSubMenuByParentId(String.valueOf(map.get("menuId")));
		for(Menu menu : menuList){
			map.put("menuId", menu.getId());
			List<String> b = menuDao.getMenuStatus(map);
			if(b.size()>0){
				menu.setHasMenu(true);
				menu.setSubMenu(this.listOperatorMenuQX(map));
				menu.setTarget("treeFrame");
			}
			
			
		}
		return menuList;
	}
	
	
	
}
